package com.example.basemodel.utils

import android.app.Activity
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Build
import android.provider.Settings
import androidx.core.app.NotificationCompat
import com.example.basemodel.R

/***
 * @author 冯杰
 * @date 2024/1/30
 * @desc
 */
object NotificationUtils {
    const val CHANNEL_ID = "wang"
    private var manager: NotificationManager? = null
    private lateinit var notification: Notification
    private lateinit var pendingIntent: PendingIntent
    fun showNotification(activity: Activity, contentTitle: String, contentText: String) {

        if (manager == null) {
            manager = activity.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        }
        //对手机系统版本进行一个判断，只有Android8.0以上才创建
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //这个时候我就去创建一个NotificationChannel
            //NotificationChannel里面有三个参数，第一id要和Notification的一致，第二个就是他的名字，可以随意设置，第三个是重要程度，需要通过NotificationManager类来设置
            val channel = NotificationChannel(CHANNEL_ID, "通知", NotificationManager.IMPORTANCE_HIGH)
            //我们创建完使用的时候，需要用Notification通知管理类把它放进去
            manager?.createNotificationChannel(channel)
        }
        val intent = Intent()
//        intent.setClassName(activity,activity.packageName+"."+activity.localClassName)
//        intent.putExtra("isClickNotification",true)
        pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0)
        notification = NotificationCompat.Builder(activity, CHANNEL_ID)
            .setContentTitle(contentTitle)
            .setContentText(contentText) //通知图标应该使用纯色的图片，因为Android从5.0系统开始，对于图标设计进行了修改，所有应用程序
            //的通知栏图标，应该使用alpha图层，其实就是没有颜色的图片
            .setSmallIcon(R.mipmap.logo) //给小图标设置颜色
            .setColor(Color.parseColor("#ff0000")) //设置大图标
            .setLargeIcon(BitmapFactory.decodeResource(activity.resources, R.mipmap.logo)) //点击通知时跳转
            .setContentIntent(pendingIntent) //设置点击通知后清除通知
            .setAutoCancel(true) //设置通知被创建的时间  不设置的话就显示的是当前系统时间
            // .setWhen()
            .build()
        //发送通知
        manager?.notify(1, notification);
    }

    /**
     * 判断是否跳转到通知模块
     * @param context Context
     */
    fun isOpenNotification(context: Context): Boolean {
        try {
            val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                if (mNotificationManager.getNotificationChannel(CHANNEL_ID) == null) {
                    val channel = NotificationChannel(CHANNEL_ID, "通知", NotificationManager.IMPORTANCE_HIGH)
                    //我们创建完使用的时候，需要用Notification通知管理类把它放进去
                    mNotificationManager?.createNotificationChannel(channel)
                }
                return mNotificationManager.getNotificationChannel(CHANNEL_ID).importance == NotificationManager.IMPORTANCE_HIGH
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return false
    }

    /**
     * 打开通知
     * @param context Context
     */
    fun openSettingNotification(context: Context) {
        val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //Android 8.0及以上
            val channel = mNotificationManager.getNotificationChannel(CHANNEL_ID) //CHANNEL_ID是自己定义的渠道ID
            //未开启
            // 跳转到设置页面
            val intent: Intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
            intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
            intent.putExtra(Settings.EXTRA_CHANNEL_ID, if (channel == null) CHANNEL_ID else channel.id)
            context.startActivity(intent)
        }
    }
}